package com.sso.core.ssoauth.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sso.core.ssoauth.entity.SysRole;
import com.sso.core.ssoauth.entity.SysUser;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * describe:
 *
 * @author 库琦
 * @date 2020/11/21/20:44
 */
@Data
public class LoginAppUser  extends SysUser implements UserDetails {
    private Set<SysRole> sysRoles;
    private Set<String> permission;
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> collection = new HashSet<>();
        if (!CollectionUtils.isEmpty(sysRoles)){
            sysRoles.forEach(sysRole -> {
                if (sysRole.getRoleCode().startsWith("ROLE_")){
                    collection.add(new SimpleGrantedAuthority(sysRole.getRoleCode()));
                }else {
                    collection.add(new SimpleGrantedAuthority("ROLE_" + sysRole.getRoleCode()));
                }
            });
            if (!CollectionUtils.isEmpty(permission)){
                permission.forEach(per->{
                    collection.add(new SimpleGrantedAuthority(per));
                });
            }
        }
        return collection;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }


}
